#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define XH(i,a,b) for(int i = a; i < b; ++i)
#define XXH(i,b,a) for(int i = b; i >= a; --i)
#define Module 998244353
int tInt;ll tll;string tString;char tChar;
vector<int>tIVector;

int n;
vector<ll>speeds;

ll go(int right, int left)
{
  ll ret = 0;
  int direction = 0;
  while(1){
    ret += speeds[right] - speeds[left];
    printf("ret = %lld\n", ret);
    if(right == n-1 && left == 0)break;
    if(right == 0 && left == n - 1)break;
    if(left == 0){
      ++right;
    }else if(right == 0){
      --left;
    }else if(direction == 1){
      --left;
      direction = -1;
    }else if(direction == -1){
      ++right;
      direction = 1;
    }else if(speeds[right] - speeds[left - 1] < speeds[right + 1] - speeds[left]){
      --left;
      direction = -1;
    }else{
      ++right;
      direction = 1;
    }
  }

  return ret;
}

void solve(){
  int right,left;
  ll sum = 0;
  if(n == 1){
    printf("0");
    return;
  }else if(n == 2){
    printf("%lld",abs(speeds[0]-speeds[1]));
    return;
  }
  if(n%2 == 0){
    right = n/2;
    left = right - 1;
  }else{
    tInt = n/2; 
    if(speeds[tInt+1] - speeds[tInt] < speeds[tInt] - speeds[tInt - 1]){
      right = tInt + 1;
      left = tInt;
    }else{
      right = tInt;
      left = tInt - 1;
    }
  }  
  sum = go(right,left);
  printf("%lld\n",sum);
}

int main()
{
#ifndef ONLINE_JUDGE
  auto start_clock = clock();
  //freopen("input.txt","r",stdin);
  //  freopen("output.txt","w",stdout);
#endif
cin>>n;
speeds.resize(n);
  XH(i,0,n){
    scanf("%lld",&speeds[i]);
  }
  sort(speeds.begin(),speeds.end());
  solve();

#ifndef ONLINE_JUDGE
  auto end_clock = clock();
  //cout<<"runtime: "<<end_clock - start_clock<<"ms"<<endl;
#endif
  return 0;
}
